/************************************************************************
  * Copyright(c) 2023 Levetop Semiconductor Co.,Led. All rights reserved.
  * @file     tp_ctp.h
  * @author   UartTFT Application Team
  * @version  V0.0.1
  * @date     2023-01-01
  * @brief    
 *************************************************************************/

#ifndef _TP_CTP_H
#define _TP_CTP_H

#include "delay.h"
#include "data.h"
#include "bsp.h"

/*--------------------------------------------CTP GPIO ---------------------------------*/
#if (LT_TOUCH_FLAG == 2)

#if (LT_PERI_SELECT == 1)

#define CTP_SDA_OUTPUT      EPORT0->EPDDR |= (1<<7)
#define CTP_SDA_INPUT       EPORT0->EPDDR &=~(1<<7)
#define CTP_SDA_PULLUP      EPORT0->EPPUE |= (1<<7)
#define CTP_GET_SDA         ((EPORT0->EPPDR>>7)&0x01)
#define CTP_SDA_HIGH        EPORT0->EPDR |= (1<<7)
#define CTP_SDA_LOW         EPORT0->EPDR &=~(1<<7)

#define CTP_SCL_OUTPUT      EPORT0->EPDDR |= (1<<6)
#define CTP_SCL_INPUT       EPORT0->EPDDR &=~(1<<6)
#define CTP_SCL_PULLUP      EPORT0->EPPUE |= (1<<6)
#define CTP_GET_SCL         ((EPORT0->EPPDR>>6)&0x01)
#define CTP_SCL_HIGH        EPORT0->EPDR |= (1<<6)
#define CTP_SCL_LOW         EPORT0->EPDR &=~(1<<6)

#define CTP_RST_OUTPUT      EPORT0->EPDDR |= (1<<4)
#define CTP_RST_PULLUP      EPORT0->EPPUE |= (1<<4)
#define CTP_RST_HIGH        EPORT0->EPDR |= (1<<4)
#define CTP_RST_LOW         EPORT0->EPDR &=~(1<<4)

#define CTP_INT_OUTPUT      EPORT0->EPDDR |= (1<<5)
#define CTP_INT_INPUT       EPORT0->EPDDR &=~(1<<5)
#define CTP_INT_PULLUP      EPORT0->EPPUE |= (1<<5)
#define CTP_INT_INTERRUPT	  
#define CTP_GET_INT         ((EPORT0->EPPDR>>5)&0x01)
#define CTP_INT_HIGH        EPORT0->EPDR |= (1<<5)
#define CTP_INT_LOW         EPORT0->EPDR &=~(1<<5)

#elif (LT_PERI_SELECT == 2)

#define CTP_SDA_OUTPUT      I2C->U8_I2CDDR.SDA = 1
#define CTP_SDA_INPUT       I2C->U8_I2CDDR.SDA = 0
#define CTP_SDA_PULLUP      I2C->U8_I2CPCR.PUI2C |= (1<<1)
#define CTP_GET_SDA         I2C->U8_I2CPDR.SDA
#define CTP_SDA_HIGH        I2C->U8_I2CPDR.SDA = 1
#define CTP_SDA_LOW         I2C->U8_I2CPDR.SDA = 0

#define CTP_SCL_OUTPUT      I2C->U8_I2CDDR.SCL = 1
#define CTP_SCL_INPUT       I2C->U8_I2CDDR.SCL = 0
#define CTP_SCL_PULLUP      I2C->U8_I2CPCR.PUI2C |= (1<<0)
#define CTP_GET_SCL         I2C->U8_I2CPDR.SCL
#define CTP_SCL_HIGH        I2C->U8_I2CPDR.SCL = 1
#define CTP_SCL_LOW         I2C->U8_I2CPDR.SCL = 0

#define CTP_RST_OUTPUT      EPORT0->EPDDR |= (1<<4)
#define CTP_RST_PULLUP      EPORT0->EPPUE |= (1<<4)
#define CTP_RST_HIGH        EPORT0->EPDR |= (1<<4)
#define CTP_RST_LOW         EPORT0->EPDR &=~(1<<4)

#define CTP_INT_OUTPUT      EPORT0->EPDDR |= (1<<5)
#define CTP_INT_INPUT       EPORT0->EPDDR &=~(1<<5)
#define CTP_INT_PULLUP      EPORT0->EPPUE |= (1<<5)
#define CTP_GET_INT         ((EPORT0->EPPDR>>5)&0x01)
#define CTP_INT_HIGH        EPORT0->EPDR |= (1<<5)
#define CTP_INT_LOW         EPORT0->EPDR &=~(1<<5)

#else

#define CTP_SDA_OUTPUT      0
#define CTP_SDA_INPUT       0
#define CTP_SDA_PULLUP      0
#define CTP_GET_SDA         0
#define CTP_SDA_HIGH        0
#define CTP_SDA_LOW         0

#define CTP_SCL_OUTPUT      0
#define CTP_SCL_INPUT       0
#define CTP_SCL_PULLUP      0
#define CTP_GET_SCL         0
#define CTP_SCL_HIGH        0
#define CTP_SCL_LOW         0

#define CTP_RST_OUTPUT      0
#define CTP_RST_PULLUP      0
#define CTP_RST_HIGH        0
#define CTP_RST_LOW         0

#define CTP_INT_OUTPUT      0
#define CTP_INT_INPUT       0
#define CTP_INT_PULLUP      0
#define CTP_GET_INT         0
#define CTP_INT_HIGH        0
#define CTP_INT_LOW         0

#endif

#define CT_COM_OK (0)
#define CT_ACK_FAIL (1)
#define CT_WRITE_MASK (0x00)
#define CT_READ_MASK (0x01)
#define CT_CACK_TIMEOUT (3000) // Wait for ACK timeout

extern uint8_t CTP_ADDR;
extern uint8_t READ_BUFF[];
extern uint8_t ctp_active_index;
extern uint8_t ctp_press_t;

// IO IIC
extern uint8_t CTI2C_Start(void);
extern uint8_t CTI2C_Stop(void);
extern uint8_t CTI2C_write_byte(const uint8_t s);
extern uint8_t CTI2C_read_byte(uint8_t ack);
extern uint8_t CTP_BufferWrite(uint8_t *pBuffer, uint32_t WriteAddr, uint8_t AddrByte, uint16_t NumByte);
extern uint8_t CTP_BufferRead(uint8_t *pBuffer, uint32_t ReadAddr, uint8_t AddrByte, uint16_t NumByte);

extern uint8_t CTP_ConfigureBuff[];
void Checksum(uint8_t *p);
void ClearBuff(uint8_t *p);
uint8_t TP_Get_INT(void);

uint8_t TP_read_XY(void);
uint8_t CTP_Scan(void);
void CTP_IO_Init(void);
void CTP_Init(void);

#endif

#endif /* _TP_CTP_H */
